/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xpath;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.w3c.dom.Document;
import org.w3c.tidy.Tidy;

/**
 *
 * @author PeSu
 */
public class ConvertXML {
   
    public static  void xhtmlparser(String file)
    { 
    try( InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(file);
         FileOutputStream fos = new FileOutputStream("tmp.xml");) 
    {
        Tidy tidy = new Tidy();
        tidy.setShowWarnings(true);
        tidy.setInputEncoding("UTF-8");
        tidy.setOutputEncoding("UTF-8");
        tidy.setXHTML(true);
        tidy.setMakeClean(true);
        Document xmlDoc = tidy.parseDOM(in, fos);
    } catch (Exception e) {
        e.printStackTrace();
    }

    try( InputStream in = new FileInputStream("text.html");
         FileOutputStream fos = new FileOutputStream("dd.xml");) {
        Tidy tidy = new Tidy();
        tidy.setShowWarnings(true);
        tidy.setXmlTags(true);
        tidy.setInputEncoding("UTF-8");
        tidy.setOutputEncoding("UTF-8");
        tidy.setXHTML(true);
        tidy.setMakeClean(true);
        Document xmlDoc = tidy.parseDOM(in, null);
        tidy.pprint(xmlDoc, fos);
    } catch (Exception e) {
        e.printStackTrace();
    }

    }
    public static  String cleanData(String data)  
    {
        try {
            Tidy tidy = new Tidy();
            tidy.setInputEncoding("UTF-8");
            tidy.setOutputEncoding("UTF-8");
            tidy.setPrintBodyOnly(true); // only print the content
            tidy.setXmlOut(true); // to XML
            tidy.setSmartIndent(true);
            ByteArrayInputStream inputStream = new ByteArrayInputStream(data.getBytes("UTF-8"));
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            tidy.parseDOM(inputStream, outputStream);
            return outputStream.toString("UTF-8");
        } catch (UnsupportedEncodingException ex) {
            return ex.getMessage();
        }
    }
    public static void HTML_to_XHTML()
    {       
      try{
         FileInputStream FIS=new FileInputStream("text.html");
         FileOutputStream FOS=new FileOutputStream("testXHTML.xml");   
         Tidy T=new Tidy();
         Document D=T.parseDOM(FIS,FOS);
         }
      catch (java.io.FileNotFoundException e)
         {System.out.println(e.getMessage());}   
      }
   
}
